package com.hcy.StudiosData;

import com.entity.Studios;


import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class StudiosAppFrame extends JDialog {

    private JLabel jl_title;//标题

    private JPanel jp_north;//上部
    private JPanel jp_center;//中部
    private JPanel jp_south;//下部


    private JPanel jp_west;//左边
    private JPanel jp_east;//右边

    private JLabel jl_HallName;//大厅标签
    private JTextField jtf_HallName;//大厅文本域

    private JLabel jl_CinemaAddress;//电影院地址标签
    private JTextField jtf_CinemaAddress;

    private JLabel jl_MovieTitle;//电影片名标签
    private JTextField jtf_MovieTitle;//文本域

    private JLabel jl_startTime;//开始时间标签
    private JTextField jtf_startTime;

    private JLabel jl_Price;//价格标签
    private JTextField jtf_Price;//

    private JLabel jl_surplus;//剩余余额标签
    private JTextField jtf_surplus;

    private JLabel jl_CinemaName;//电影院名称
    private JTextField jtf_CinemaName;

    private JButton jbt_submit;//提交

    private StudiosFrame studiosFrame;

    private int id;

    public StudiosAppFrame(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        this.studiosFrame = (StudiosFrame) owner;
        this.getContentPane().setBackground(Color.WHITE);
        setLayout(new BorderLayout());


        createContent();

        jp_north =  new JPanel();
        jl_title = new JLabel(title);
        jp_north.setBackground(Color.WHITE);
        setLabel(jl_title);
        jp_north.add(jl_title);
        add(jp_north, BorderLayout.NORTH);

        jp_center =  new JPanel();
        jp_center.setLayout(new BorderLayout());

        add(jp_center, BorderLayout.CENTER);

        jp_south =  new JPanel();
        jp_south.add(jbt_submit);
        jp_south.setBackground(Color.WHITE);
        add(jp_south, BorderLayout.SOUTH);


        jp_west = new JPanel();
        jp_west.setLayout(new GridLayout(7, 1, 20, 20));
        jp_west.setPreferredSize(new Dimension(130, 0));
        jp_west.setBackground(Color.WHITE);
        jp_center.add(jp_west, BorderLayout.WEST);
        jp_west.add(jl_HallName);
        jp_west.add(jl_CinemaAddress);
        jp_west.add(jl_MovieTitle);
        jp_west.add(jl_startTime);
        jp_west.add(jl_Price);
        jp_west.add(jl_surplus);
        jp_west.add(jl_CinemaName);

        jp_east = new JPanel();
        jp_east.setLayout(new GridLayout(7, 1, 20, 20));
        jp_east.setBackground(Color.WHITE);
        jp_center.add(jp_east, BorderLayout.CENTER);

        jp_east.add(jtf_HallName);
        jp_east.add(jtf_CinemaAddress);
        jp_east.add(jtf_MovieTitle);
        jp_east.add(jtf_startTime);
        jp_east.add(jtf_Price);
        jp_east.add(jtf_surplus);
        jp_east.add(jtf_CinemaName);

        if (this.studiosFrame.getPnStudios()!= null) {
            Studios studios = this.studiosFrame.getPnStudios();
            id = studios.getSid();
            jtf_HallName.setText(studios.getsHallName());
            jtf_CinemaAddress.setText(studios.getsCinemaAddress());
            jtf_MovieTitle.setText(studios.getsMovieTitle());
            jtf_startTime.setText(studios.getSstartTime());
            jtf_Price.setText(studios.getsPrice()+"");
            jtf_surplus.setText(studios.getSsurplus()+"");
            jtf_CinemaName.setText(studios.getsCinemaName());
        }

        //1.先把窗口显示出来
        //标题
        setTitle("增删改查");
        //尺寸
        setSize(600,600);
        //居中
        setLocationRelativeTo(null);
        //设置关闭模式。直接结束整个程序
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //设置可见
        setVisible(true);

    }

    public void createContent() {
        jl_HallName = new JLabel("大厅:");
        setLabel(jl_HallName);
        jtf_HallName = new JTextField();

        jl_CinemaAddress = new JLabel("电影院地址：");
        setLabel(jl_CinemaAddress);
        jtf_CinemaAddress = new JTextField();

        jl_MovieTitle = new JLabel("电影名称：");
        setLabel(jl_MovieTitle);
        jtf_MovieTitle = new JTextField();


        jl_startTime = new JLabel("播放时间:");
        setLabel(jl_startTime);
        jtf_startTime = new JTextField();


        jl_Price = new JLabel("价格(数字):");
        setLabel(jl_Price);
        jtf_Price = new JTextField();
        //限制输入的只能为数字
        jtf_Price.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char keyChar = e.getKeyChar();
                if (keyChar >= '0' && keyChar<= '9'){
                    return;

                }
                Toolkit.getDefaultToolkit().beep();

                e.consume();
            }
        });




        jl_surplus = new JLabel("剩余(数字):");
        setLabel(jl_surplus);
        jtf_surplus = new JTextField();
        //限制输入的只能为数字
        jtf_surplus.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char keyChar = e.getKeyChar();
                if (keyChar >= '0' && keyChar<= '9'){
                    return;

                }
                Toolkit.getDefaultToolkit().beep();

                e.consume();
            }
        });

        jl_CinemaName = new JLabel("电影院名称:");
        setLabel(jl_CinemaName);
        jtf_CinemaName = new JTextField();


        jbt_submit = new JButton("提交");
        jbt_submit.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Studios studios = getStudios();
                System.out.println(studios);
                int i = 0;
                if (studiosFrame.getPnStudios() == null) {
                    //当前用户没有值，增加
                    i = studiosFrame.getStudiosDao().insertStudios(studios);
                } else {
                    i = studiosFrame.getStudiosDao().updateStudios(studios);
                }

                Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(StudiosAppFrame.this, i>0?"提交成功":"提交失败");
                //关闭自己
                dispose();
            }
        });

    }

    public Studios getStudios() {
        String HallName = jtf_HallName.getText();
        String CinemaAddress = jtf_CinemaAddress.getText();
        String MovieTitle = jtf_MovieTitle.getText();
        String startTime = jtf_startTime.getText();
        double Price = Double.parseDouble(jtf_Price.getText());
        int surplus = Integer.parseInt(jtf_surplus.getText());
        String CinemaName = jtf_CinemaName.getText();

        return new Studios(id,HallName,CinemaAddress,MovieTitle,startTime,Price,surplus,CinemaName);
    }

    public void setLabel(JLabel jl) {
        //水平居中
        jl.setHorizontalAlignment(SwingConstants.RIGHT);
        //垂直居中
        jl.setVerticalAlignment(SwingConstants.CENTER);
        //设置字体
        jl.setFont(new Font("微软雅黑", Font.PLAIN, 18));

    }

    public static void main(String[] args) {

    }

}
